package com.bw.mvvm_common.network.inteceptor;

import com.bw.mvvm_common.utils.SPUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 接口中的token拦截器
 * */
public class TokenInterceptor implements Interceptor {
    /**
     * 获取原有request
     * 获取sp中存放的token字符串
     * 创建新的request
     * request 添加header-token ： sp存放服务器下发的token字符串
     * chain.proceed()
     * */
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        String token = SPUtils.readToken();
        Request.Builder builder = new Request.Builder();
        builder.url(request.url());
        builder.headers(request.headers());
        builder.addHeader("token",token);
        builder.post(request.body());
        return chain.proceed(builder.build());
    }
}
